#! /bin/bash

#cmd=radosgw -c /etc/ceph/ceph.conf -n client.radosgw.node7-1
proc=radosgw

function getpid() {
        #local pid=$(ps -ef | grep "$exe" | grep -v "grep" | grep -v "$0" | awk '{print $2}')
	pid=$(ps -C $proc -o pid=)
	(( ${#pid} == 0 )) && pid=0
	echo "pid=$pid"
        return $pid
}


function start() {
	radosgw -c /etc/ceph/ceph.conf -n client.radosgw.node7-1
        ret=$?
        if (($ret == 0))
        then
                echo "sucess to start $proc"
        else
                echo "failed to start $proc"
        fi
        return $ret
}

function status() {
        getpid
        if (( $pid > 0 ))
        then
                echo "$proc is running..."
                return 0
        else
                echo "$proc is not running"
                return 1
        fi
}

function stop() {
        getpid
        if (( $pid > 0 ))
        then
                kill $pid
                if (($? == 0))
                then
                        echo "success to stop $proc"
                        return 0
                else
                        echo "failed to stop $proc"
                        return 1
                fi
        else
                echo "$proc is not running"
                return 0
        fi
}

function restart() {
        stop
        if (($? == 1))
        then
                exit 3
        fi

	sleep 1

        start
        if (($? != 0))
        then
                exit 4
        fi
}

case "$1" in
        "start")
                start && exit $?
        ;;
        "status")
                status  && exit $?
        ;;
        stop)
                stop  && exit $?
        ;;
        restart)
                restart  && exit $?
        ;;
esac

